{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3xW4O0zuKfWD"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from openai import OpenAI\n",
        "from google.colab import userdata"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "api_key = userdata.get('OPENAI_API_KEY')\n",
        "MODEL = \"gpt-4o-mini\"\n",
        "\n",
        "openai = OpenAI(api_key=api_key)"
      ],
      "metadata": {
        "id": "vUydnLeLKs03"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Chat Completion API\n",
        "\n",
        "https://platform.openai.com/docs/guides/streaming-responses?api-mode=chat"
      ],
      "metadata": {
        "id": "5wlqKfVBKyjD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "prompts = [\n",
        "    { \"role\": \"user\", \"content\": \"Tell me a joke about the internet\"}\n",
        "]\n",
        "\n",
        "stream = openai.chat.completions.create(\n",
        "    model=MODEL,\n",
        "    messages= prompts,\n",
        "    stream=True\n",
        ")"
      ],
      "metadata": {
        "id": "dBrUkMCUKz0s"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(stream)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WItchxDLK2x6",
        "outputId": "7ab0fa87-1071-4db5-c930-dac3e99d9a21"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "<openai.Stream object at 0x7ea9b934f0d0>\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "for chunk in stream:\n",
        "        print(chunk)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "V2OvZRgFK38l",
        "outputId": "63e08d72-31fe-47ee-af25-a819b8fc4b8a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content='', function_call=None, refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content='Why', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' did', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' the', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' computer', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' go', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' to', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' therapy', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content='?\\n\\n', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content='Because', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' it', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' had', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' too', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' many', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' cookies', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' and', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=\" couldn't\", function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' clear', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' its', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=' cache', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content='!', function_call=None, refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n",
            "ChatCompletionChunk(id='chatcmpl-BESdUWbo9O8B5Oyq2K6bH2LfmV7lL', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None), finish_reason='stop', index=0, logprobs=None)], created=1742786368, model='gpt-4o-mini-2024-07-18', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_b8bc95a0ac', usage=None)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "prompts = [\n",
        "    { \"role\": \"user\", \"content\": \"Tell me a joke about the internet\"}\n",
        "]\n",
        "\n",
        "stream = openai.chat.completions.create(\n",
        "    model=MODEL,\n",
        "    messages= prompts,\n",
        "    stream=True\n",
        ")\n",
        "\n",
        "response = \"\"\n",
        "for chunk in stream:\n",
        "        print(f\"Content: '{chunk.choices[0].delta.content}'\")\n",
        "        print(\"Finish Reason:\", chunk.choices[0].finish_reason) # Noties the finish reason in last chunk\n",
        "        response += chunk.choices[0].delta.content or ''\n",
        "\n",
        "print()\n",
        "print(\"Response\")\n",
        "print(response)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Y0fE_ns_JuE3",
        "outputId": "efab0226-b5c2-4d4b-914f-525f59ab8de8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Content: ''\n",
            "Finish Reason: None\n",
            "Content: 'Why'\n",
            "Finish Reason: None\n",
            "Content: ' did'\n",
            "Finish Reason: None\n",
            "Content: ' the'\n",
            "Finish Reason: None\n",
            "Content: ' computer'\n",
            "Finish Reason: None\n",
            "Content: ' go'\n",
            "Finish Reason: None\n",
            "Content: ' to'\n",
            "Finish Reason: None\n",
            "Content: ' therapy'\n",
            "Finish Reason: None\n",
            "Content: '?\n",
            "\n",
            "'\n",
            "Finish Reason: None\n",
            "Content: 'Because'\n",
            "Finish Reason: None\n",
            "Content: ' it'\n",
            "Finish Reason: None\n",
            "Content: ' had'\n",
            "Finish Reason: None\n",
            "Content: ' too'\n",
            "Finish Reason: None\n",
            "Content: ' many'\n",
            "Finish Reason: None\n",
            "Content: ' tabs'\n",
            "Finish Reason: None\n",
            "Content: ' open'\n",
            "Finish Reason: None\n",
            "Content: ' and'\n",
            "Finish Reason: None\n",
            "Content: ' couldn't'\n",
            "Finish Reason: None\n",
            "Content: ' resolve'\n",
            "Finish Reason: None\n",
            "Content: ' its'\n",
            "Finish Reason: None\n",
            "Content: ' issues'\n",
            "Finish Reason: None\n",
            "Content: '!'\n",
            "Finish Reason: None\n",
            "Content: 'None'\n",
            "Finish Reason: stop\n",
            "\n",
            "Response\n",
            "Why did the computer go to therapy?\n",
            "\n",
            "Because it had too many tabs open and couldn't resolve its issues!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Responses API\n",
        "\n",
        "https://platform.openai.com/docs/guides/streaming-responses?api-mode=responses"
      ],
      "metadata": {
        "id": "tCzSosP5K-JR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "stream = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a joke about the internet\",\n",
        "    stream=True\n",
        ")"
      ],
      "metadata": {
        "id": "2IPLSF30K--S"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(stream)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zIxOV55hLJoz",
        "outputId": "0e3635d0-e497-4f2c-cee2-b5aa9d74eb79"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "<openai.Stream object at 0x7ea9b934ef10>\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "In Responses API's stream we receive events not just delta content like in Chat API. So we need to pick specific event for our need\n",
        "\n",
        "https://platform.openai.com/docs/api-reference/responses-streaming"
      ],
      "metadata": {
        "id": "1eX5shNyJ7zw"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = \"\"\n",
        "for event in stream:\n",
        "        print(event)  # Notice the name of events"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2jbA57efLF8z",
        "outputId": "555bfefc-91cf-456d-9424-121a9411a682"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "ResponseCreatedEvent(response=Response(id='resp_67e0cf4fca4881928dcc4e6dcc92686d0008c6129c5589ca', created_at=1742786383.0, error=None, incomplete_details=None, instructions=None, metadata={}, model='gpt-4o-mini-2024-07-18', object='response', output=[], parallel_tool_calls=True, temperature=1.0, tool_choice='auto', tools=[], top_p=1.0, max_output_tokens=None, previous_response_id=None, reasoning=Reasoning(effort=None, generate_summary=None), status='in_progress', text=ResponseTextConfig(format=ResponseFormatText(type='text')), truncation='disabled', usage=None, user=None, store=True), type='response.created')\n",
            "ResponseInProgressEvent(response=Response(id='resp_67e0cf4fca4881928dcc4e6dcc92686d0008c6129c5589ca', created_at=1742786383.0, error=None, incomplete_details=None, instructions=None, metadata={}, model='gpt-4o-mini-2024-07-18', object='response', output=[], parallel_tool_calls=True, temperature=1.0, tool_choice='auto', tools=[], top_p=1.0, max_output_tokens=None, previous_response_id=None, reasoning=Reasoning(effort=None, generate_summary=None), status='in_progress', text=ResponseTextConfig(format=ResponseFormatText(type='text')), truncation='disabled', usage=None, user=None, store=True), type='response.in_progress')\n",
            "ResponseOutputItemAddedEvent(item=ResponseOutputMessage(id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', content=[], role='assistant', status='in_progress', type='message'), output_index=0, type='response.output_item.added')\n",
            "ResponseContentPartAddedEvent(content_index=0, item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, part=ResponseOutputText(annotations=[], text='', type='output_text'), type='response.content_part.added')\n",
            "ResponseTextDeltaEvent(content_index=0, delta='Why', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' did', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' the', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' computer', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' go', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' to', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' therapy', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta='?\\n\\n', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta='Because', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' it', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=\" couldn't\", item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' stop', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' browsing', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' for', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta=' attention', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDeltaEvent(content_index=0, delta='!', item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, type='response.output_text.delta')\n",
            "ResponseTextDoneEvent(content_index=0, item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, text=\"Why did the computer go to therapy?\\n\\nBecause it couldn't stop browsing for attention!\", type='response.output_text.done')\n",
            "ResponseContentPartDoneEvent(content_index=0, item_id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', output_index=0, part=ResponseOutputText(annotations=[], text=\"Why did the computer go to therapy?\\n\\nBecause it couldn't stop browsing for attention!\", type='output_text'), type='response.content_part.done')\n",
            "ResponseOutputItemDoneEvent(item=ResponseOutputMessage(id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', content=[ResponseOutputText(annotations=[], text=\"Why did the computer go to therapy?\\n\\nBecause it couldn't stop browsing for attention!\", type='output_text')], role='assistant', status='completed', type='message'), output_index=0, type='response.output_item.done')\n",
            "ResponseCompletedEvent(response=Response(id='resp_67e0cf4fca4881928dcc4e6dcc92686d0008c6129c5589ca', created_at=1742786383.0, error=None, incomplete_details=None, instructions=None, metadata={}, model='gpt-4o-mini-2024-07-18', object='response', output=[ResponseOutputMessage(id='msg_67e0cf50237c819299f12125eac8c1db0008c6129c5589ca', content=[ResponseOutputText(annotations=[], text=\"Why did the computer go to therapy?\\n\\nBecause it couldn't stop browsing for attention!\", type='output_text')], role='assistant', status='completed', type='message')], parallel_tool_calls=True, temperature=1.0, tool_choice='auto', tools=[], top_p=1.0, max_output_tokens=None, previous_response_id=None, reasoning=Reasoning(effort=None, generate_summary=None), status='completed', text=ResponseTextConfig(format=ResponseFormatText(type='text')), truncation='disabled', usage=ResponseUsage(input_tokens=32, output_tokens=17, output_tokens_details=OutputTokensDetails(reasoning_tokens=0), total_tokens=49, input_tokens_details={'cached_tokens': 0}), user=None, store=True), type='response.completed')\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Just showing type of event and 'response.output_text.delta' event will have content"
      ],
      "metadata": {
        "id": "hz1yTSBUKJUJ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "stream = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a joke about the internet\",\n",
        "    stream=True\n",
        ")\n",
        "\n",
        "response = \"\"\n",
        "for event in stream:\n",
        "        print(event.type) # Just showing type of event and 'response.output_text.delta' event will have content\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gK2ZsaQKKKLz",
        "outputId": "2edb0b53-cbc5-4c1a-b467-89706551a8d8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "response.created\n",
            "response.in_progress\n",
            "response.output_item.added\n",
            "response.content_part.added\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.delta\n",
            "response.output_text.done\n",
            "response.content_part.done\n",
            "response.output_item.done\n",
            "response.completed\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "stream = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a joke about the internet\",\n",
        "    stream=True\n",
        ")\n",
        "\n",
        "response = \"\"\n",
        "for event in stream:\n",
        "        if event.type == \"response.output_text.delta\":\n",
        "            response += event.delta\n",
        "            print(\"Delta Content: \", event.delta)\n",
        "print(\"Response\")\n",
        "print(response)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MhrgrD-uKOVW",
        "outputId": "7d012303-4b55-4821-d5b0-2e30ec96bde7"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Delta Content:  Why\n",
            "Delta Content:   did\n",
            "Delta Content:   the\n",
            "Delta Content:   computer\n",
            "Delta Content:   go\n",
            "Delta Content:   to\n",
            "Delta Content:   therapy\n",
            "Delta Content:  ?\n",
            "\n",
            "\n",
            "Delta Content:  Because\n",
            "Delta Content:   it\n",
            "Delta Content:   had\n",
            "Delta Content:   too\n",
            "Delta Content:   many\n",
            "Delta Content:   bytes\n",
            "Delta Content:   from\n",
            "Delta Content:   all\n",
            "Delta Content:   the\n",
            "Delta Content:   drama\n",
            "Delta Content:   on\n",
            "Delta Content:   social\n",
            "Delta Content:   media\n",
            "Delta Content:  !\n",
            "Response\n",
            "Why did the computer go to therapy?\n",
            "\n",
            "Because it had too many bytes from all the drama on social media!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "stream = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a joke about the internet\",\n",
        "    stream=True\n",
        ")\n",
        "\n",
        "response = \"\"\n",
        "for event in stream:\n",
        "        # Only difference in if condition\n",
        "        if \"output_text.delta\" in event.type:\n",
        "            response += event.delta\n",
        "            print(\"Delta Content: \", event.delta)\n",
        "print(\"Response\")\n",
        "print(response)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kT7da-NkKQXv",
        "outputId": "3a84afdc-76e2-4d4a-b94b-598217a85610"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Delta Content:  Why\n",
            "Delta Content:   did\n",
            "Delta Content:   the\n",
            "Delta Content:   computer\n",
            "Delta Content:   break\n",
            "Delta Content:   up\n",
            "Delta Content:   with\n",
            "Delta Content:   the\n",
            "Delta Content:   internet\n",
            "Delta Content:  ?\n",
            "\n",
            "\n",
            "Delta Content:  Because\n",
            "Delta Content:   it\n",
            "Delta Content:   found\n",
            "Delta Content:   someone\n",
            "Delta Content:   more\n",
            "Delta Content:   secure\n",
            "Delta Content:  !\n",
            "Response\n",
            "Why did the computer break up with the internet?\n",
            "\n",
            "Because it found someone more secure!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "stream = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a joke about the internet\",\n",
        "    stream=True\n",
        ")\n",
        "\n",
        "response = \"\"\n",
        "for event in stream:\n",
        "        if hasattr(event, \"response\"):\n",
        "              print(event.response.status)\n",
        "        if event.type == \"response.completed\":\n",
        "            response = event.response.output_text\n",
        "print()\n",
        "print(\"Response\")\n",
        "print(response)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wHL-xWXtKR9v",
        "outputId": "e31a838c-bfc5-4627-bf41-960859521812"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "in_progress\n",
            "in_progress\n",
            "completed\n",
            "\n",
            "Response\n",
            "Why did the computer go to therapy?\n",
            "\n",
            "Because it had too many unresolved issues!\n"
          ]
        }
      ]
    }
  ]
}